package org.spring.springboot.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.spring.springboot.domain.ErrorInfo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;

@ControllerAdvice
public class GlobalExceptionHandler {
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public ErrorInfo<Object> jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		ErrorInfo<Object> r = new ErrorInfo<Object>();
		r.setMessage(e.getMessage());
		r.setCode(ErrorInfo.ERROR);
		r.setMethod(req.getMethod());
		r.setData(req.getParameterMap());
		r.setUrl(req.getRequestURL().toString());
		return r;
	}
}